home *** CD-ROM | disk | FTP | other *** search
- ## -*-Tcl-*- (nowrap)
- # ==========================================================================
- # FILE: "setextMode.tcl"
- #
- # Setext file support
- #
- # Recognize and automatically mark 'setext'-encoded text files, like Tidbits.
- #
- # created: 10/01/94 {09:51:15 pm}
- # last update: 12/28/2000 {09:53:09 AM}
- #
- # Author: Craig Barton Upright
- # E-mail: <cupright@princeton.edu>
- # mail: Princeton University
- # 2.N.1 Green Hall, Princeton, New Jersey 08544
- # www: <http://www.princeton.edu/~cupright>
- #
- # -------------------------------------------------------------------
- #
- # Copyright (c) 2000 Tom Pollard, Craig Barton Upright
- #
- # This program is free software; you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation; either version 2 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program; if not, write to the Free Software
- # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- #
- # ==========================================================================
- ##
-
- # ===========================================================================
- #
- # Explanation of what Setext is and does
- # ======================================
- #
- # Setext stands for Structure Enhanced Text. It is a markup scheme for
- # plain text documents such as email messages and e-zines. Setext's
- # primary goal is to provide a way of marking text that is visually
- # unobtrusive, so that if you don't have a special setext browser, like
- # EasyView, you can still read the text. (Have you ever tried to make
- # sense of HTML source without your web browswer?)
- #
- # The document Setext Help is a description of setext concepts written by
- # Ian Feldman. Setext grabbed a foothold in the Mac world with the online
- # publication TidBITS. Rudimentary setext browsers were built with
- # HyperCard for reading TidBITS. Setext seems to be merely a historical
- # curiousity now.
- #
- # (NOTE: Setext is easier to use with mono-spaced fonts like Monoco.)
- #
- # -Donavan Hall, Tuesday, March 14, 2000
- #
- # ------------------------------------------------------------------
- #
- # Version 1.2 of Setx mode
- # ==========================
- #
- # After Donavan sent me a Setext file for the Mode Examples folder, with an
- # excellent description of what Setext is (was?), I have developed a
- # fondness for the mode and use it exclusively for various README.TXT files
- # that I often write to annotate my work -- as Donavan mentions, the
- # marking function is quite handy. I decided to update the mode, allowing
- # the user to set additional preferences, including some comment character,
- # magic character, keyword definition and colorizing.
- #
- # In some ways, Setx could be thought of as "Text2", with the same
- # functionality, but greater customization available. If Alpha does not
- # have a mode that users need, Setx could be adapted to serve as a
- # surrogate until they've convinced someone to write one for them. The
- # current version of Setx, orginally written by Tom Pollard, is the
- # long-awaited 1.2 .
- #
- # The default mode preferences are intended to show off some of Setx's new
- # functionality, which can be observed in the Setext Help file.
- #
- # For more information see the Setext Help file.
- #
- # - Craig Barton Upright, 12 April, 2000
- #
-
- # ===========================================================================
- #
- # ◊◊◊◊ Initialization of Setx mode ◊◊◊◊ #
- #
-
- alpha::mode Setx 2.0 SetxMenu {*.stx *.etx} {
- electricReturn
- } {
- # We require 7.4b21 for prefs handling.
- alpha::package require AlphaTcl 7.4b21
- } uninstall {
- this-file
- } help {
- file "Setext Help"
- } maintainer {
- "Craig Barton Upright" <cupright@princeton.edu>
- <http://www.princeton.edu/~cupright/>
- }
-
- proc setextMode.tcl {} {}
-
- proc SetxMenu {} {}
-
- namespace eval Setx {}
-
- # ===========================================================================
- #
- # ◊◊◊◊ Setting Setx mode variables ◊◊◊◊ #
- #
- # I tried to write this mode allowing the user to make any necessary
- # modifications through the Mode Prefs dialog. The commented explanations
- # given above each of the following preferences will appear when the user
- # clicks the "Help" button of the dialog.
- #
- # Setting comment, magic character, and keyword preferences to show off
- # Setx's capabilities in the Setxt Help file.
- #
-
- # Removing obsolete preferences from earlier versions.
-
- set oldvars {
- don'tRemindMe
- }
-
- foreach oldvar $oldvars {prefs::removeObsolete SetxmodeVars($oldvar)}
-
- unset oldvars
-
- #=============================================================================
- #
- # Standard preferences recognized by various Alpha procs
- #
-
- newPref flag autoMark {0} Setx
- newPref var fillColumn {75} Setx
- newPref var wordBreak {\w+} Setx
- newPref var wordBreakPreface {(\W)} Setx
- newPref flag wordWrap {1} Setx
-
- #=============================================================================
- #
- # Flag preferences
- #
-
- # Check this box to use the defined paired comment characters in Comment
- # Line / Box / Paragraph menu items.
- newPref flag usePairedComments {0} Setx {Setx::updatePreferences}
-
- #=============================================================================
- #
- # Variable preferences
- #
-
- # Everything from the Comment Character(s) to the end of the current line
- # will be colorized according to the "Comment Color". This should agree
- # with the Prefix String below.
- newPref var commentCharacter {#} Setx {Setx::updatePreferences}
-
- # Select the opening character(s) of a bracketed comment.
- newPref var commentPair1 {/*} Setx {Setx::updatePreferences}
-
- # Select the ending character(s) of a bracketed comment. These cannot be
- # the same as the opening characters.
- newPref var commentPair2 {*/} Setx {Setx::updatePreferences}
-
- # Define the indentation string for Section marks.
- newPref var indentString { } Setx
-
- # Setx allows for three levels of keywords. Shorter lists can be entered
- # here in the preferences. For longer lists, see the Setext Help file for
- # instructions on editing a SetxPrefs.tcl file.
- newPref var keywords1 {setx} Setx {Setx::updatePreferences}
- newPref var keywords2 {} Setx {Setx::updatePreferences}
- newPref var keywords3 {See the Setext Help file for more information.} Setx {Setx::updatePreferences}
-
- # Magic Characters will colorize any string which follows them, using the
- # "symbol" color. Only one Magic Character can be defined.
- newPref var magicCharacter {$} Setx {Setx::updatePreferences}
-
- # Select a Prefix String for commenting lines. This should agree with the
- # Comment Character above, but also have a space after the character.
- newPref var prefixString {# } Setx
-
- # Command double-clicking will send the highlighted text to this search
- # engine.
- newPref url searchUrl1 {http://www.google.com/search?q=} Setx
-
- # Command double-clicking while pressing the "option" key will send the
- # highlighted text to this search engine.
- newPref url searchUrl2 {http://www.go.com/Split?sv=IS&lk=noframes&qt=} Setx
-
- # Command double-clicking while pressing the "control" key will send the
- # highlighted text to this search engine.
- newPref url searchUrl3 {http://search.metacrawler.com/crawler?general=} Setx
-
- # Command double-clicking while pressing the "shift" key will send the
- # highlighted text to this search engine.
- newPref url searchUrl4 {http://northernlight.com/nlquery.fcg?si=&cb=0&qr=} Setx
-
- # Additional characters to be colorized by the "Symbol Color". The "-" and
- # "=" symbols will always be included.
- newPref var symbols {@ %} Setx {Setx::updatePreferences}
-
- # ===========================================================================
- #
- # Color preferences
- #
- # Since I want to put a message in the keyword3 box, I am setting that
- # color black (which will show up as "none").
- #
-
- newPref color commentColor {red} Setx {Setx::updatePreferences}
- newPref color keyword1Color {magenta} Setx {Setx::updatePreferences}
- newPref color keyword2Color {black} Setx {Setx::updatePreferences}
- newPref color keyword3Color {black} Setx {Setx::updatePreferences}
-
- # Color of the user defined magic character.
- newPref color magicColor {blue} Setx {Setx::updatePreferences}
-
- # Strings are any words that appear between double quotes on the same line.
- newPref color stringColor {green} Setx {Setx::updatePreferences}
-
- # This preference colorizes the = and - strings which indicate that the
- # line above is a heading or subheading, and any other symbols defined by
- # the user in "Symbols".
- newPref color symbolColor {blue} Setx {Setx::updatePreferences}
-
- # ===========================================================================
- #
- # Update Preferences.
- #
- # This allows for changes to take effect without a restart.
- #
- # Danger: Don't include this proc in any {mode}Prefs.tcl file !!!
- #
- # This will source the prefs file, and thus put Alpha in an endless loop.
- # Instead, use a <mode>::colorize<mode> proc in the prefs file.
- #
-
- proc Setx::updatePreferences {flag} {
-
- global mode PREFS $flag SetxmodeVars
-
- # If the mode has a {mode}Prefs.tcl file, we want to load that as
- # well, otherwise any keywords contained therein won't be updated
- # without a manual "Load Prefs File".
-
- if {[file exists [file join ${PREFS} ${mode}Prefs.tcl]]} {
- uplevel #0 [list source [file join ${PREFS} ${mode}Prefs.tcl]]
- }
-
- Setx::colorizeSetx
- Setx::commentMenuItems
- refresh
-
- # Alertnote notes after certain preferences have been changed. Once a
- # keyword has been identified, it cannot be "unloaded" until a restart.
-
- if {($flag == "keywords1") ||
- ($flag == "keywords2") ||
- ($flag == "keywords3") ||
- ($flag == "symbols") } {
-
- alertnote "Deletions from $flag preference\
- will only take effect after a restart.\
- Keywords cannot be 'unloaded.'"
- }
- }
-
- # ===========================================================================
- #
- # Comment Character variables for Comment Line / Paragraph / Box menu items.
- #
-
- proc Setx::commentMenuItems {} {
-
- global SetxmodeVars Setx::commentCharacters
-
- if {$SetxmodeVars(usePairedComments)} {
-
- # determining what the "middle comment character" is in a pair.
-
- if {[string length $SetxmodeVars(commentPair1)] == 2} {
- set mCC [string index $SetxmodeVars(commentPair1) 1]
- } else {
- set mCC {*}
- }
-
- set Setx::commentCharacters(General) \
- "$mCC"
-
- set Setx::commentCharacters(Paragraph) \
- [list "$SetxmodeVars(commentPair1) " \
- " $SetxmodeVars(commentPair2)" \
- " $mCC "]
-
- set Setx::commentCharacters(Box) \
- [list "$SetxmodeVars(commentPair1)" 1 \
- "$SetxmodeVars(commentPair2)" 1 \
- "$mCC" 3]
-
- } else {
- set cC $SetxmodeVars(commentCharacter)
- set Setx::commentCharacters(General) \
- "$cC "
-
- set Setx::commentCharacters(Paragraph) \
- [list "$cC$cC " \
- " $cC$cC" \
- " $cC "]
-
- set Setx::commentCharacters(Box) \
- [list "$cC" 1 \
- "$cC" 1 \
- "$cC" 3]
- }
- }
-
- # Call this now.
-
- Setx::commentMenuItems
-
- # ===========================================================================
- #
- # ◊◊◊◊ Colorize Setx ◊◊◊◊ #
- #
- #
- # Used in updatePreferences, and could be called in a <mode>Prefs.tcl file
- #
-
- proc Setx::colorizeSetx {} {
-
- global SetxmodeVars Setxcmds
-
- set Setxcmds [lsort [lunique [concat \
- $SetxmodeVars(keywords1) \
- $SetxmodeVars(keywords2) \
- $SetxmodeVars(keywords3) \
- ]]]
-
- # Keywords 1
-
- regModeKeywords -a \
- -e $SetxmodeVars(commentCharacter) \
- -b $SetxmodeVars(commentPair1) \
- $SetxmodeVars(commentPair2) \
- -c $SetxmodeVars(commentColor) \
- -s $SetxmodeVars(stringColor) \
- -k $SetxmodeVars(keyword1Color) Setx \
- $SetxmodeVars(keywords1)
-
- # Keywords 2
-
- regModeKeywords -a \
- -k $SetxmodeVars(keyword2Color) Setx \
- $SetxmodeVars(keywords2)
-
- # Keywords 3
-
- regModeKeywords -a \
- -k $SetxmodeVars(keyword3Color) Setx \
- $SetxmodeVars(keywords3)
-
- # Symbols, Magic Character
-
- regModeKeywords -a \
- -m $SetxmodeVars(magicCharacter) \
- -k $SetxmodeVars(magicColor) Setx \
- $SetxmodeVars(symbols) \
- -i "=" -i "-" \
- -I $SetxmodeVars(symbolColor)
- }
-
- # This is a "dummy" command, necessary for the above proc so that all of
- # the "regModeKeywords" commands in the called color procs can be "adds"
- # (-a). When the mode is first invoked, this has to occur before the color
- # procs are called.
-
- regModeKeywords -k {none} Setx {}
-
- # now we finally colorize
-
- Setx::colorizeSetx
-
- # ===========================================================================
- #
- # ◊◊◊◊ Key Bindings, Electrics ◊◊◊◊ #
- #
-
- Bind '-' <c> {Setx::underline -} Setx
- Bind '=' <c> {Setx::underline =} Setx
-
- proc Setx::underline {symbol} {
-
- goto [set pos [lineStart [getPos]]]
- endLineSelect
-
- # First convert all tabs to spaces.
- tabsToSpaces
- goto $pos
- endLineSelect
-
- # Now remove any stray spaces from the end of the current line.
- replaceText $pos [selEnd] [string trimright [getSelect]]
- goto $pos
- endLineSelect
-
- # Now substitute the symbol for any character, and insert a new
- # line below the current one.
- regsub -all {.} [getSelect] $symbol symbolLine
- goto [selEnd]
- insertText "\r${symbolLine}\r"
- markFile
- }
-
- # Setting the order of precedence for completions.
-
- set completions(Setx) {
- completion::cmd completion::electric completion::word
- }
-
- # Using any defined keywords for completions
-
- set Setxcmds [lsort [concat \
- $SetxmodeVars(keywords1) $SetxmodeVars(keywords2) $SetxmodeVars(keywords3)]]
-
-
- # ===========================================================================
- #
- # ◊◊◊◊ Command Double Click ◊◊◊◊ #
- #
- # Send the highlighted text to the defined search engine.
- #
-
- proc Setx::DblClick {from to shift option control} {
-
- global SetxmodeVars
-
- select $from $to
- set command [getSelect]
- regsub -all { } $command {+} commandPlus
- set commandPlus [concat %22$commandPlus%22]
-
- # Any modifiers pressed?
- if {$option && $SetxmodeVars(searchUrl2) != ""} {
- message "\"$command\" sent to $SetxmodeVars(searchUrl2)"
- url::execute $SetxmodeVars(searchUrl2)$commandPlus
- } elseif {$control && $SetxmodeVars(searchUrl3) != ""} {
- message "\"$command\" sent to $SetxmodeVars(searchUrl3)"
- url::execute $SetxmodeVars(searchUrl3)$commandPlus
- } elseif {$shift && $SetxmodeVars(searchUrl4) != ""} {
- message "\"$command\" sent to $SetxmodeVars(searchUrl4)"
- url::execute $SetxmodeVars(searchUrl4)$commandPlus
- } elseif {$SetxmodeVars(searchUrl1) != ""} {
- message "\"$command\" sent to $SetxmodeVars(searchUrl1)"
- url::execute $SetxmodeVars(searchUrl1)$commandPlus
- } else {
- message "The search url preference for this modifier has not been set."
- }
- }
-
- # ===========================================================================
- #
- # ◊◊◊◊ Mark File Proc ◊◊◊◊ #
- #
- #
- # author: Tom Pollard
- #
- # Changes made by cbu:
- #
- # -- changes the sub-heading to only indent three spaces, not four
- # -- strip comment character, spaces from the beginning of any mark name
- #
- # How the Mark File works
- #
- # Any two lines that look like this:
- #
- # Any string of words
- # ===================
- #
- # will be marked as a Chapter heading. Any two lines that look like this:
- #
- # Any other string of words
- # -------------------------
- #
- # will be marked as a Section heading. That's all there is to it.
- #
-
- proc Setx::MarkFile {} {
-
- removeAllMarks
- message "Marking File …"
- global SetxmodeVars
-
- set cC $SetxmodeVars(commentCharacter)
- set iS $SetxmodeVars(indentString)
-
- set pat1 {^(-+|=+)$}
- set end [maxPos]
- set pos [minPos]
- set count1 0
- set count2 0
- set l {}
- while {![catch {search -s -f 1 -r 1 -m 0 -i 1 $pat1 $pos} match]} {
- set pos1 [lineStart [pos::math [lindex $match 0] - 1]]
- set marker [string trimright [eval getText $match]]
- set line [string trimright [getText $pos1 [nextLineStart $pos1]]]
- if {[string length $line] == [string length $marker]} {
- # Strip any leading comment characters, spaces from the mark
- set line [string trimleft $line "$cC "]
- if {[string range $marker 0 0] == "-"} {
- set line "$iS$line"
- incr count2
- } else {
- incr count1
- }
- regsub {/} $line {-} line
- set inds($line) $pos1
- lappend sects $line
- }
- set pos [nextLineStart [lindex $match 1]]
- }
-
- if {[info exists inds]} {
- foreach f $sects {
- set next [lineStart $inds($f)]
- setNamedMark $f $inds($f) $next $next
- }
- }
- message "This file contains $count1 chapters, $count2 sections."
- }
-
- # ===========================================================================
- #
- # ◊◊◊◊ Version History ◊◊◊◊ #
- #
- # modified by rev reason
- # -------- --- ------ -----------
- # 10/01/94 tp 1.0.1 First version of Setx mode written by Tom Pollard
- # 04/02/00 cbu 1.0.2 Additional preferences added, allowing user to define a
- # comment character, magic character, keyword dictionaries
- # 04/06/00 cbu 1.1 Added "Update Colors" proc to avoid need for a restart
- # 04/20/00 cbu 1.1.1 Added "Use Paired Comments" variable for menu items.
- # Added "Comment Menu Items" proc to update commentCharacter sets.
- # 06/22/00 cbu 1.2 Reorganized Color proc routines.
- # Renamed "Update Colors" to "Update Preferences".
- # Fixed the "middle comment character" dilemna in paired
- # comments.
- # Moved "refresh" from Colorize to Update Preferences to
- # avoid "no open window" bug from ever coming up.
- # Mark names are stripped of leading comment characters,
- # spaces. This way one can colorize headings using
- # comment character.
- # Section marks indentation now a variable.
- # 12/04/00 cbu 1.3 Added Setx::DblClick for search urls.
- # 12/04/00 cbu 2.0 New url prefs handling requires 7.4b21
- # Added Bernard's bindings, Setx::underline
- #
-
- # ===========================================================================
- #
- # .